<template>
	<view class="v-content">
		<view class="rate-wapper">商品分数: <uni-rate v-model="GoodsGrade" activeColor="#00BFFF" @change="onChange($event,'GoodsGrade')" /></view>
		<view class="rate-wapper">服务分数: <uni-rate v-model="ServiceGrade" activeColor="#00BFFF" @change="onChange($event,'ServiceGrade')" />
		</view>
		<view v-if="orderDetail.Type==2" class="rate-wapper">物流分数: <uni-rate v-model="LogisticsScore" activeColor="#00BFFF" @change="onChange($event,'LogisticsScore')" /></view>
		<view class="v-title">评价内容:</view>
		<view class="textarea-waper">
			<textarea class="v-textarea" v-model="cause" maxlength="200" />
		</view>
		<view class="v-title">上传图片(最多上传3张)</view>
		<robby-image-upload v-model="imageData" limit="3" :server-url-delete-image="true"
			:server-url="true"></robby-image-upload>
		<view class="bottom-wapper">
			<view class="bottom-btn" @tap.stop="submit">提交</view>
		</view>

	</view>
</template>

<script>
	import robbyImageUpload from '@/mall_subpages/components/robby-image-upload/robby-image-upload.vue'
	import unirRate from '@/components/uni-rate/uni-rate.vue'
	import {
		addEvaluation,
		getUserOrderList
	} from '@/api/mall.js'
	export default {
		components: {
			robbyImageUpload,
			unirRate
		},
		data() {
			return {
				productIndex: 0,
				isSubmit: false,
				imageData: [],
				cause: '',
				timer: null,
				GoodsGrade: 5,
				ServiceGrade: 5,
				LogisticsScore: 5,
				orderDetail: null,
			};
		},
		onLoad(options) {
			this.productIndex = options.index;
			this.getOrderDetail(options.id);
		},
		methods: {
			// 获取订单详情
			async getOrderDetail(orderId) {
				try {
					uni.showLoading({
						title: '加载中...',
						mask: true
					});

					const { Data } = await getUserOrderList({
						OrderId: orderId
					});

					if (Data && Data.length > 0) {
						this.orderDetail = Data[0];
					} else {
						uni.showToast({
							title: '订单不存在',
							icon: 'none'
						});
						setTimeout(() => {
							uni.navigateBack();
						}, 1500);
					}
				} catch (error) {
					console.error('获取订单详情失败:', error);
					uni.showToast({
						title: '获取订单失败',
						icon: 'none'
					});
				} finally {
					uni.hideLoading();
				}
			},
			submit() {
				if (!this.orderDetail) {
					uni.showToast({
						title: '订单数据加载中',
						icon: 'none'
					});
					return;
				}

				console.log(this.imageData)
				let _this = this
				if (this.cause.length <= 0) {
					uni.showToast({
						title: '请输入评价类容',
						icon: 'none'
					})
				} else {
					if (this.isSubmit) return
					let infoId = 0;
          console.log(this.orderDetail)
					console.log(this.orderDetail.OrdersOnlineInfoViewList)
					if (this.orderDetail.ProductType === 2) {
						infoId = this.orderDetail.OrdersOnlineInfoViewList[0].Id
					} else {
						infoId = this.orderDetail.OrdersOnlineInfoViewList[0].ProductOrdersCouponInfoModel.Id
					}
					let parma = {
						"OrdersOnLineId": this.orderDetail.OrdersOnlineInfoViewList[0].ProductOrdersId,
						"InfoId": infoId,
						"ProductId": this.orderDetail.OrdersOnlineInfoViewList[0].ProductId,
						"Message": this.cause,
						"ImgList": this.imageData,
						"GoodsGrade": this.GoodsGrade,
						"ServiceGrade": this.ServiceGrade,
						"LogisticsScore": this.LogisticsScore,
						"Status": 1,
						"PrantId": '',
						"businessid": this.orderDetail.BusinessId
					}
					// parma.OrdersOnLineId = 7804
					// parma.InfoId = 8120
					uni.showLoading({
						title: '提交中',
						mask: true
					})
					this.isSubmit = true
					addEvaluation(parma).then(res => {
						uni.hideLoading();
						uni.showModal({
							title: '提示',
							content: '评论成功',
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									try {} catch (e) {}
									uni.navigateBack()
								} else if (res.cancel) {}
							}
						});
					}).catch(rej => {
						uni.showToast({
							title: rej.Msg,
							icon: 'none'
						})
						this.isSubmit = false
					})
				}
			},
			deleteImage(e) {},
			addImage(e) {},
			onChange(e, type) {
				console.log("评价", e, type)
				this[type] = e.value
			}
		},
		beforeDestroy() {
			clearTimeout(this.timer)
		}
	}
</script>

<style lang="scss" scoped>
	.v-content {
		padding: 20upx;
	}

	.v-title {
		padding: 20upx 0;
		font-size: 32rpx;
	}

	.v-textarea {
		box-sizing: border-box;
		padding: 20upx;
		background: $uni-bg-color-grey;
		width: 100%;
		height: 200upx;
	}

	.cell-pd {
		padding: 22upx 30upx;
	}

	.list-pd {
		margin-top: 50upx;
	}

	.bottom-wapper {
		position: fixed;
		width: 100%;
		z-index: 10;
		bottom: 0;
		left: 0;
		color: #fff;
		padding: 20upx;

		.bottom-btn {
			width: 100%;
			padding: 20upx;
			text-align: center;
			border-radius: 50upx;
			font-size: $uni-font-size-base;
			background: #498FFF;
		}
	}

	.rate-wapper {
		display: flex;
		align-items: center;
		padding: 20rpx 0;
		font-size: $uni-font-size-lg;

		/deep/ .uni-rate {
			padding-left: 20rpx;
			padding-top: 20rpx;
		}
	}
</style>
